home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / ANSI.SWG / 0001_Re: Object oriented ANSI Unit.pas
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  5.3 KB  |  228 lines

  1. {
  2.  TF> Hello . How Are YOU doing.
  3.  
  4.  TF> I have some trouble and i'm wondering if you can help me.
  5.  TF> Can you write a little program that showes ansi-pics under
  6.  TF> Turbo Pascal 7.
  7.  
  8. Here's a UNIT of mine that interprets and displays ANSI files:
  9.  
  10. ----------------------------------------------- }
  11. {Written by Mike Phillips 12-01-93}
  12.  
  13. UNIT ANSI;
  14. INTERFACE
  15.  
  16. USES crt,dos;
  17.  
  18.  
  19. VAR
  20.   speed : integer;
  21.  
  22. FUNCTION Min(X,Y : integer):integer;
  23. FUNCTION Max(X,Y : INTEGER) : INTEGER;
  24.  
  25.  
  26. TYPE
  27.   charset = set of char;
  28.   TANSiView = object
  29.     ANSiParams : array [byte] of byte;
  30.     ANSiNum : byte;
  31.     errornum : integer;
  32.     ANSiType : char;
  33.     ANS : text;
  34.     ANSiStr : string;
  35.     ESCChars : charset;
  36.     blinkin,high : boolean;
  37.     fore,back,temp : word;
  38.     constructor init;
  39.     procedure attrib; virtual;
  40.     procedure parse; virtual;
  41.     procedure read_file (fn:string); virtual;
  42.     procedure err; virtual;
  43.     destructor done; virtual;
  44.   end; (* of object *)
  45.  
  46.  
  47.  
  48. IMPLEMENTATION
  49.  
  50. FUNCTION Min(X,Y : INTEGER) : INTEGER; ASSEMBLER;
  51. ASM
  52.    MOV   AX,X
  53.    CMP   AX,Y
  54.    JLE   @@1
  55.    MOV   AX,Y
  56. @@1:
  57. END;
  58.  
  59. FUNCTION Max(X,Y : INTEGER) : INTEGER; ASSEMBLER;
  60. ASM
  61.    MOV   AX,X
  62.    CMP   AX,Y
  63.    JGE   @@1
  64.    MOV   AX,Y
  65. @@1:
  66. END;
  67.  
  68.  
  69. constructor TANSiView.Init;
  70. begin
  71.   clrscr;
  72.   blinkin := false;
  73.   high := false;
  74.   fore := 7;
  75.   back := 0;
  76.   ESCChars :=
  77. ['H','f','A','B','C','D','s','u','J','K','m','h','l','p','@',#14];end; (* of
  78. constructor TANSiVeiw.Init *)
  79. procedure TANSiView.attrib;
  80. var
  81.   count : byte;
  82. begin
  83.   FOR count := 1 TO ANSiParams [0] DO begin
  84.     case ANSiParams [count] of
  85.       0 : begin
  86.             blinkin := false;
  87.             high := false;
  88.             fore := 7;
  89.             back := 0;
  90.             end;
  91.       1 : high := true;
  92.       4,7 : begin
  93.               temp := fore;
  94.               fore := back;
  95.               back := temp;
  96.             end;
  97.       5 : blinkin := true;
  98.       8 : fore := back;
  99.       30 : fore := black;
  100.       31 : fore := red;
  101.       32 : fore := green;
  102.       33 : fore := brown;
  103.       34 : fore := blue;
  104.       35 : fore := magenta;
  105.       36 : fore := cyan;
  106.       37 : fore := lightgray;
  107.       40 : back := black;
  108.       41 : back := red;
  109.       42 : back := green;
  110.       43 : back := brown;
  111.       44 : back := blue;
  112.       45 : back := magenta;
  113.       46 : back := cyan;
  114.       47 : back := lightgray;
  115.     end; (* of CASE *)
  116.   END; (* of FOR *)
  117.   if high then fore := fore OR 8;
  118.   if blinkin then textattr := fore + back*16 + 128
  119.   else textattr := fore + back*16;
  120. end; (* of method attrib *)
  121.  
  122. procedure TANSiView.parse;
  123. type
  124.   tcurpos = record
  125.     x,y : byte
  126.   end; (* of record *)
  127. var
  128.   count : byte;
  129.   tempst : string;
  130.   inpu : char;
  131.   curpos : tcurpos;
  132. begin
  133.   ANSiParams [0] := 0;
  134.   read (ans,inpu);
  135.   if inpu <> '[' then exit;
  136.   WHILE NOT (inpu in ESCChars) DO begin
  137.   read (ans,inpu);
  138.   WHILE NOT (inpu in [';'] + ESCChars) DO begin
  139.     if inpu in ['0'..'9'] then tempst := tempst + inpu;
  140.     read (ans,inpu);
  141.   end; (* of WHILE *)
  142.   val (tempst,ANSiParams[ANSiParams[0]+1],errornum);
  143.   if errornum = 0 then inc (ANSiParams [0]);
  144.   tempst := '';
  145.   if inpu in ESCChars then begin
  146.     case inpu of
  147.       'H','f' : IF ANSiParams[0] = 2 then gotoxy (ANSiParams[2],ANSiParams[1])
  148.                 else IF ANSiParams[0] = 0 then gotoxy (1,1)
  149.                 else gotoxy (1,ansiparams[1]);
  150.       'A' : IF ANSIParams [0] = 0 then if wherey > 1 then gotoxy (wherex,wherey-1)
  151.       else if wherey > ANSiParams [1] then gotoxy (wherex,wherey-ANSiParams[1])
  152.       else gotoxy (wherex,1);
  153.  
  154.       'B' : IF ANSiParams [0] = 0 then gotoxy (wherex,wherey+1)
  155.             else gotoxy (wherex,wherey+ANSiParams[1]);
  156.  
  157.       'C' : IF ANSiParams [0] = 0 then gotoxy (wherex+1,wherey)
  158.             else gotoxy (wherex+ANSiParams[1],wherey);
  159.       'D' : IF ANSIParams [0] = 0 then gotoxy (wherex-1,wherey)
  160.             else if wherex > ANSiParams [1] then gotoxy (wherex -
  161. ANSiParams[1],wherey)            else gotoxy (1,wherey);
  162.       's' : begin
  163.                curpos.x := wherex;
  164.                curpos.y := wherey;
  165.                end;
  166.       'u' : gotoxy (curpos.x,curpos.y);
  167.       'J' : clrscr;
  168.       'K' : clreol;
  169.       'm' : attrib;
  170.     end; (* of case *)
  171.   end; (* of IF *)
  172.   end; (* of WHILE *)
  173. end; (* of method parse *)
  174.  
  175. procedure TANSiView.read_file (Fn:string);
  176. var
  177.   ch:char;
  178. begin
  179. {$I-}
  180.   ANSiStr := Fn;
  181.   assign (ANS,Fn);
  182.   reset (ANS);
  183. {$I+}
  184.   if ioresult <> 0 then begin
  185.     err;
  186.     exit;
  187.   end;
  188.   read (ANS,ch);
  189.   while not eof (ans) do begin
  190.     delay (speed);
  191.     if ch = #27 then parse
  192.     else write (ch);
  193.     read (ans,ch);
  194.   end; (* of while *)
  195.   close (ans);
  196. end; (* of method read_file *)
  197.  
  198. procedure TANSiView.err;
  199. begin
  200.   writeln ('File not found');
  201. end;
  202.  
  203. destructor TANSiView.done;
  204. begin
  205. end;
  206.  
  207. begin
  208.   speed := 0;
  209. end.
  210.  
  211. { TEST PROGRAM ----------------------------------------------}
  212.  
  213. Use it like :
  214. var
  215.   MyANSI : TANSIView;
  216.   ANSIFile : string;
  217. begin
  218.   write ('Enter the filename to view:  ');
  219.   readln (ANSIFile);
  220.   MyANSI.Read_File (ANSIFile);
  221. end.
  222.  
  223. You may set the speed variable to slow down the display in order to
  224. emulate various baud rates.  Higher value = slower.
  225.  
  226. Mike Phillips
  227. INTERNET:  phil4086@utdallas.edu
  228.